/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import DAL.ConnectionDB;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Pham
 */
public class JFMain extends javax.swing.JFrame {

    /**
     * Creates new form JFMain
     */
    
    public JFMain() {
        
        initComponents();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        pnShow = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnSystem = new javax.swing.JMenu();
        mnILogin = new javax.swing.JMenuItem();
        mnILogout = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnIExit = new javax.swing.JMenuItem();
        mnMenu = new javax.swing.JMenu();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnIProduct = new javax.swing.JMenuItem();
        mnIImport = new javax.swing.JMenuItem();
        mnIOrder = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        mnIInventory = new javax.swing.JMenuItem();
        mnItemNumberImport = new javax.swing.JMenuItem();
        mnItemExport = new javax.swing.JMenuItem();
        mnReport = new javax.swing.JMenu();
        ImnReportRevenue = new javax.swing.JMenuItem();
        mnHelp = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        javax.swing.GroupLayout pnShowLayout = new javax.swing.GroupLayout(pnShow);
        pnShow.setLayout(pnShowLayout);
        pnShowLayout.setHorizontalGroup(
            pnShowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 950, Short.MAX_VALUE)
        );
        pnShowLayout.setVerticalGroup(
            pnShowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 550, Short.MAX_VALUE)
        );

        mnSystem.setText("Hệ thống");

        mnILogin.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, java.awt.event.InputEvent.CTRL_MASK));
        mnILogin.setText("Đăng nhập");
        mnILogin.setToolTipText("Đăng nhập vào hệ thống");
        mnSystem.add(mnILogin);

        mnILogout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, java.awt.event.InputEvent.CTRL_MASK));
        mnILogout.setText("Đăng xuất");
        mnSystem.add(mnILogout);
        mnSystem.add(jSeparator1);

        mnIExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        mnIExit.setText("Thoát chương trình");
        mnIExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnIExitActionPerformed(evt);
            }
        });
        mnSystem.add(mnIExit);

        jMenuBar1.add(mnSystem);

        mnMenu.setText("Danh mục");

        jMenu1.setText("Sản Phẩm");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Loại Sản Phẩm");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        mnIProduct.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mnIProduct.setText("Sản phẩm");
        mnIProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnIProductActionPerformed(evt);
            }
        });
        jMenu1.add(mnIProduct);

        mnMenu.add(jMenu1);

        mnIImport.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        mnIImport.setText("Phiếu Nhập");
        mnIImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnIImportActionPerformed(evt);
            }
        });
        mnMenu.add(mnIImport);

        mnIOrder.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        mnIOrder.setText("Đơn hàng");
        mnIOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnIOrderActionPerformed(evt);
            }
        });
        mnMenu.add(mnIOrder);

        jMenuBar1.add(mnMenu);

        jMenu3.setText("Thống kê");

        mnIInventory.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.ALT_MASK));
        mnIInventory.setText("Tồn kho");
        mnIInventory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnIInventoryActionPerformed(evt);
            }
        });
        jMenu3.add(mnIInventory);

        mnItemNumberImport.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK));
        mnItemNumberImport.setText("Số lượng nhập");
        mnItemNumberImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemNumberImportActionPerformed(evt);
            }
        });
        jMenu3.add(mnItemNumberImport);

        mnItemExport.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.ALT_MASK));
        mnItemExport.setText("Số lượng bán");
        mnItemExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemExportActionPerformed(evt);
            }
        });
        jMenu3.add(mnItemExport);

        jMenuBar1.add(jMenu3);

        mnReport.setText("Báo cáo");

        ImnReportRevenue.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        ImnReportRevenue.setText("Doanh thu trong tháng");
        ImnReportRevenue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ImnReportRevenueActionPerformed(evt);
            }
        });
        mnReport.add(ImnReportRevenue);

        jMenuBar1.add(mnReport);

        mnHelp.setText("Trợ giúp");
        jMenuBar1.add(mnHelp);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnShow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnShow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 29, Short.MAX_VALUE))
        );

        pack();
    }//GEN-END:initComponents

    private void mnIExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnIExitActionPerformed
        
    }//GEN-LAST:event_mnIExitActionPerformed

    private void mnIProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnIProductActionPerformed
        pnShow.removeAll();
        JPanelManageProduct pn = new JPanelManageProduct();
        pn.setSize(new Dimension(940, 560));
        pnShow.add(pn);
        revalidate();
    }//GEN-LAST:event_mnIProductActionPerformed

    //method action import
    private void mnIImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnIImportActionPerformed
        pnShow.removeAll();
        JPanelMangeImport pnImport = new JPanelMangeImport();
        pnImport.setSize(new Dimension(940, 560));
        pnShow.add(pnImport);
        revalidate();
    }//GEN-LAST:event_mnIImportActionPerformed

    //method action inventory
    private void mnIInventoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnIInventoryActionPerformed
       pnShow.removeAll();
       JPanelInventory pn = new JPanelInventory();
       pn.setSize(new Dimension(940, 560));
       pnShow.add(pn);
       revalidate();
    }//GEN-LAST:event_mnIInventoryActionPerformed

    //method action manufacture
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JPanelManageManufacture manu = new JPanelManageManufacture();
        pnShow.removeAll();
        manu.setSize(new Dimension(940, 560));
        pnShow.add(manu);
        revalidate();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    //method action order
    private void mnIOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnIOrderActionPerformed
        pnShow.removeAll();
        JPanelManageOrder pnOrder = new JPanelManageOrder();
        pnOrder.setSize(new Dimension(940, 560));
        pnShow.add(pnOrder);
        revalidate();
    }//GEN-LAST:event_mnIOrderActionPerformed

    //method action statictis import
    private void mnItemNumberImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemNumberImportActionPerformed
        JPanelStatisticImport panel = new JPanelStatisticImport();
        pnShow.removeAll();
        panel.setSize(new Dimension(940, 560));
        pnShow.add(panel);
        revalidate();
    }//GEN-LAST:event_mnItemNumberImportActionPerformed

    private void mnItemExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemExportActionPerformed
        JPanelStatisticExport panel = new JPanelStatisticExport();
        pnShow.removeAll();
        panel.setSize(new Dimension(940, 560));
        pnShow.add(panel);
        revalidate();
    }//GEN-LAST:event_mnItemExportActionPerformed

    private void ImnReportRevenueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ImnReportRevenueActionPerformed
        try{
            ConnectionDB conn = new ConnectionDB();
            //String source = "D:\\Learning\\Java\\Projects\\ProjectManageShopComputer2\\src\\Report\\ReportRevenue.jrxml";
            String source = this.getClass().getResource("../Report/ReportRevenue.jrxml").toString();
            String query = "SELECT p.product_id, p.product_name, SUM(d.quantity) as `quantity`, p.product_price " +
                        "FROM `product` p, `order` o, `order_detail` d " +
                        "WHERE p.product_id = d.product_id AND o.order_id = d.order_id AND o.order_state = 2 AND MONTH(o.order_datecreated) like '9' " +
                        "GROUP BY p.product_id ";
            PreparedStatement stmt = conn.getConnectionDB().prepareStatement(query);
            ResultSet rs = stmt.executeQuery();
            JRResultSetDataSource resultSetSource = new JRResultSetDataSource(rs);
            source = source.substring(6, source.length());
            JasperReport jr = JasperCompileManager.compileReport(source);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, resultSetSource);
            JasperViewer.viewReport(jp);
            
            
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
        }
    }//GEN-LAST:event_ImnReportRevenueActionPerformed

    /**
     * @param args the command line arguments
     */
    
    /*method resize image*/
    private ImageIcon resizeImg(ImageIcon iconIn, int width, int height)
    {
        Image img = iconIn.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        ImageIcon iconOut = new ImageIcon(img);
        return iconOut;
    }
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFMain main = new JFMain();
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                main.setLocation((dim.width - main.getWidth())/2, (dim.height - main.getHeight())/2);
                main.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ImnReportRevenue;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenu mnHelp;
    private javax.swing.JMenuItem mnIExit;
    private javax.swing.JMenuItem mnIImport;
    private javax.swing.JMenuItem mnIInventory;
    private javax.swing.JMenuItem mnILogin;
    private javax.swing.JMenuItem mnILogout;
    private javax.swing.JMenuItem mnIOrder;
    private javax.swing.JMenuItem mnIProduct;
    private javax.swing.JMenuItem mnItemExport;
    private javax.swing.JMenuItem mnItemNumberImport;
    private javax.swing.JMenu mnMenu;
    private javax.swing.JMenu mnReport;
    private javax.swing.JMenu mnSystem;
    private javax.swing.JPanel pnShow;
    // End of variables declaration//GEN-END:variables
}
